package org.sjsu.freedoor.security;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

//source: http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/
//DOESN'T QUITE WORK SINCE NEED wsctx TO BE POPUPLATED AS WEBSERVICE ENDPOINT
public class SecurityService {
	public final boolean CHECK_SECURITY = true;
	
	@Resource
    WebServiceContext wsctx;
	
	protected boolean isAdminUser() {
		if(!CHECK_SECURITY)
			return true;
		
		MessageContext mctx = wsctx.getMessageContext();

		//get detail from request headers
		Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
		List userList = (List) http_headers.get("username");
		//List passList = (List) http_headers.get("password");

		return (userList != null && userList.size() > 0
				&& userList.get(0).toString().equals("theadjs"));
	}
	
	//utility method to add authenticated user
	public static Map<String, Object> addAdminUser(Map<String, Object> ctx) {
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("username", Collections.singletonList("theadjs"));
        //headers.put("password", Collections.singletonList("password"));
        ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        
        return ctx;
	}
}
